<?php

namespace app\controller;
use app\service\BarrageService;
use think\response\Json;

class BarrageController extends BasicController
{
    public array $param;
    protected BarrageService $barrageServce;
    public function initialize()
    {
        parent::initialize();
        $this->barrageServce = new BarrageService();
        $this->param = $this->request->param();
    }

    /**
     * 获取弹幕词频
     * @api
     * @ApiParam
     */
    public function getBarrageList(): Json
    {
        $result = $this->dataValidate();
        if(false != $result) {
            return returnJson(400,$result[0],$result[1]);
        }
        return returnJson(200,"成功",$this->barrageServce->parsingBarrage($this->param["roomId"],$this->param["startDate"],$this->param["endDate"],$this->param["top"]));
    }

    /**
     * @return Json
     */
    public function getBarrageSendTop(): Json
    {
        $result = $this->dataValidate();
        if(false != $result) {
            return returnJson(400,$result[0],$result[1]);
        }
        return returnJson(200,"成功",$this->barrageServce->barrageSendTop($this->param["roomId"],$this->param["startDate"],$this->param["endDate"],$this->param["top"]));
    }
    protected function dataValidate(): bool|array
    {
        $data = [
            "roomId"    => "array",
            "startDate" => "date",
            "endDate"   => "date",
            "top"       => "number"
            ];
        $result = validateParam($this->param,$data);
        if([] != $result["missParam"]) {
            return ["缺失参数",$result["missParam"]];
        }else if([] != $result["typeError"]) {
            return ["参数类型有误",$result["typeError"]];
        }
        return false;
    }
}